<?php
/**
 * file featurerlistcontroller.php
 * Created on 2015-4-18 下午5:45:32 
 * Create by xiweijie
 */
namespace haibao\cms\business\control\pc\channel\exclusive;

use haibao\cms\business\control\PublicController;

class FeatureListController extends \haibao\cms\business\control\pc\channel\ChannelController{
    
    const FEATURE_TYPE = 1;
    const VIDEO_TYPE = 2;
    
    public function __construct($updateTime = null,$controlId,$isPublish){
        $this->savaLog();
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $temp = array();
        if($controlData['auto']){
            foreach($controlData['auto'] as $data){
                $parameterValue = json_decode($data['ParameterValue'],true);
                $temp[] = $parameterValue;
            }
        }
        //var_dump($controlData);exit;
        $articleList = array();
        if($controlData['manual']){
            $resultList = $this->getActualData($controlData['manual'],$controlData['auto']);
        }
    
        $this->assign('columnList', $temp);
        if(!empty($temp)){
            $this->assign('default', $temp[0]);
        }
        
        $domainHost = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_STATIC_DOMAIN);
        if(\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_CDN_DEFAULT)){
        	$domainHost = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_CDN_DOMAIN);
        }
        $this->assign('staticHost', $domainHost);
        $this->assign('staticVersion', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_STATIC_VERSION));
		$this->assign('exclusiveDomain', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE));
        $isNewHtml = $this->generateHtml($path,$content);
    
        $content = $this->getHtml($path);
        // 解析内容 更换链接
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    private function getActualData($dataCondition,$multiData = array()){
    	

    	$resultList = array();
    	$multiDataCount = count($multiData);
        if($dataCondition['type'] == self::FEATURE_TYPE){
        	// DO : 专题列表手动数据与自动数据相结合 最多显示6个
        	if($multiDataCount > 0){
        		$this->parseMultiData($multiData, $dataCondition);
        	}
        	$featureOldIds = array();
        	if($this->packageArrCount > 0){
        		$dataCondition['limit'] = $dataCondition['limit'] - $this->packageArrCount;
        		foreach($this->packageArr as $arr){
        			$entity = $arr['Entity'];
                	$entity->ListDate = $this->formatDate($entity->PublishTime->format('Y-m-d H:i:s'));
                	$entity->Pic = $entity->Image->Filename;
                	$entity->Title = $entity->FeatureDefine->FeatureName;
                	array_push($resultList,$entity);
                	array_push($featureOldIds,$entity->Id);
        		}
        		
        		$articleController = new \haibao\cms\business\control\ArticleController();
        		//$featureOldIds = $articleController->getOldData($featureOldIds, 'FeaturePublish');
        	}
        	
        	/* if($dataCondition['limit'] > 0){
	            $result = PublicController::getFeatureByCId(0,$dataCondition['limit'],1,$featureOldIds);
	            foreach ($result as $key=>$list){
	                $list->ListDate = $this->formatDate($list->ListDate->format('Y-m-d H:i:s'));
	                array_push($resultList,$list);
	            }
       		}  */
            
            $this->assign('siteType',3);
        
        }elseif($dataCondition['type'] == self::VIDEO_TYPE){
      
        	
        	// DO : 根据控件ID判断是否是手动数据
        	if($dataCondition['id'] == 157){
        		if($multiDataCount > 0){
        			$this->parseMultiData($multiData, $dataCondition);
        		}
        		if($this->packageArrCount > 0){
        			$dataCondition['limit'] = $dataCondition['limit'] - $this->packageArrCount;
        			foreach($this->packageArr as $arr){
        				$entity = $arr['Entity'];
        				$entity->PublishDate = $this->formatDate($entity->PublishDate->format('Y-m-d H:i:s'));
        				array_push($resultList,$arr['Entity']);
        			}
        			if($dataCondition['limit'] > 0){
        			
			            $videoList = PublicController::getVideoByCateId(0,$dataCondition['limit']);
			            foreach ($videoList as $key=>$list){
			                $list->PublishDate = $this->formatDate($list->PublishDate->format('Y-m-d H:i:s'));
        					array_push($resultList,$list);
			            }
        			}
        		}
        		
        	}else{
	            $resultList = PublicController::getVideoByCateId(0,$dataCondition['limit']);
	            foreach ($resultList as $key=>$list){
	                $list->PublishDate = $this->formatDate($list->PublishDate->format('Y-m-d H:i:s'));
	            }
        	}
            $this->assign('siteType',4);
        }

        $this->assign('resultList', $resultList);
    }
}